<?php

namespace app\api\controller\Appoint;

use app\api\controller\Common;
use app\api\Logic\Campusclass\CampusClassDateRecordLogic;
use app\api\Logic\Campusclass\ClassCourseLogic;

/**
 * 学员排队记录
 */
class ClassDateRecord extends Common
{
    protected $noNeedLogin = ['*'];

    protected $campusClassDateRecordLogic;
    protected $classCourseLogic;

    public function _initialize()
    {
        parent::_initialize();
        $this->campusClassDateRecordLogic = new CampusClassDateRecordLogic();
        $this->classCourseLogic = new ClassCourseLogic();
    }

    /**
     * 返回学员约课排队记录
     */
    public function list(){
        if($this->request->isPost()){
            $params = $this->request->param();
            //验证数据
            $params = $this->verifyData($params);

            //获取学员排队记录
            $list = $this->campusClassDateRecordLogic->getRecordList(['user_id' => $params['user_id']],true);

            foreach ($list['data'] as $key => $val){
                //根据date_id获取班课详情
                $list['data'][$key]['class_course_date_info'] = $this->classCourseLogic->getInfo($val['date_id']);
            }

            if(!empty($list)){
                $this->success('查询成功',$list);
            }
            $this->error('暂无数据');
        }
        $this->error('请求错误');
    }

    /**
     * 取消排队
     */
    public function cancel(){
        if($this->request->isPost()){
            $params = $this->request->param();
            //验证数据
            $params = $this->verifyData($params);

            $recordId = $params['record_id'] ?? 0;
            if(!$recordId){
                $this->error('排队记录ID必须');
            }

            $recordInfo = $this->campusClassDateRecordLogic->getInfo($recordId);
            if(empty($recordInfo)){
                $this->error('排队记录不存在');
            }

            if($recordInfo['status'] != 0){
                $this->error('当前状态不可取消');
            }

            $res = $this->campusClassDateRecordLogic->cancel($recordId);
            if($res){
                $this->success('取消成功');
            }
            $this->error('取消失败');
        }
        $this->error('请求错误');
    }
}
